﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Collections;
using System.Web;

namespace Ehai.Web.Framework.Rules
{
    class MsgRecoverRuleHandle : IMsgRuleHandle
    {
        private static int k = 0;
        private static int y = 1;
        private static Hashtable mytable = new Hashtable();
        private static Hashtable hash = new Hashtable();

        #region IMsgRuleHandle 成员

#if DEBUG

        private int _execStatus = 0;
        public int ExecStatus
        {
            get { return _execStatus; }
        }
#endif

        public const string Name = "msgrecover";

        public string[][] Execute(string[] p, ContextInfo ci)
        {
            for (int n = 0; n < 4; n++)
            {
                if (String.IsNullOrEmpty(p[n]))
                {
#if DEBUG
                    _execStatus = n;
#endif
                    return null;
                }
            }
            string[][] result = { new string[1] { String.Empty } };
            string path = p[1];
            string ext = p[2];
            string inFile = p[3];
            //判断要查找的目录是否存在
            if (Directory.Exists(@path) == false)
            {
                result[0][0] = "该目录不存在";
                return result;
            }

            //将要查找的文件名后缀分解存入数组中,并且分不同的文件后缀进行操作
            else
            {
                string[] arrayDom = ext.Split(',');
                XmlDocument doc = new XmlDocument();
                doc.Load(@path + "\\" + inFile);
                y = doc.GetElementsByTagName("content").Count;
                hash.Clear();
                for (; y >= 1; y--)
                {
                    hash.Add(doc.GetElementsByTagName("content")[y - 1].Attributes["id" + y].Value, doc.GetElementsByTagName("content")[y - 1].Attributes["ct" + y].Value);
                }
                y = 1;
                for (int i = 0; i < arrayDom.Length; i++)
                {
                    DirectoryInfo dir = new DirectoryInfo(@path);

                    //对每一个文件进行操作
                    FileInfo[] file = dir.GetFiles("*" + arrayDom[i]);
                    if (file.Length == 0)
                    {
                        result[0][0] = "不存在此类型的文件";
                        return result;
                    }
                    for (int j = 0; j < file.Length; j++)
                    {

                        //创建一个读取该文件的对象
                        StreamReader r = new StreamReader(@path + "\\" + file[j]);

                        //获取文件中的所有内容并将其存在textAll中
                        string textAll = r.ReadToEnd();
                        r.Close();

                        //正则表达式的参数
                        System.Text.RegularExpressions.RegexOptions options =
                            (System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace
                            | System.Text.RegularExpressions.RegexOptions.Multiline
                            | System.Text.RegularExpressions.RegexOptions.IgnoreCase
                            | System.Text.RegularExpressions.RegexOptions.Singleline);

                        //构造个正则表达式的对象
                        Regex rx = new Regex("(?:/\\*\\@\\*/\\s*\\\").*?(?:\\\"\\s*/\\*\\@\\*/)", options);
                        textAll = rx.Replace(textAll, new MatchEvaluator(replaceCC1));
                        StreamWriter w = new StreamWriter(@path + "\\" + file[j]);

                        //将内容写入到文件中去
                        w.WriteLine(textAll);
                        w.Close();
                        if (k == 0)
                            result[0][0] = "没有找到还原的内容\n";
                        else
                            result[0][0] = "共找到" + k + "个匹配项,并且还原成功!";
                    }
                }
                k = 0;

                return result;
            }
        }

        private static string replaceCC1(Match m)
        {
            //正则表达式的参数
            System.Text.RegularExpressions.RegexOptions options =
               (System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace
               | System.Text.RegularExpressions.RegexOptions.Multiline
               | System.Text.RegularExpressions.RegexOptions.IgnoreCase
               | System.Text.RegularExpressions.RegexOptions.Singleline);

            Regex r = new Regex("(?<=/\\*\\@\\*/\\s*\\\").*?(?=\\\"\\s*/\\*\\@\\*/)", options);
            Match n = r.Match(m.Value);
            k++;
            return "/*@*/\"" + (string)hash[n.Value] + "\"/*@*/";
        }


        public string GetHelpInfo()
        {
            return "将加密的请求消息进行还原";
        }

        public string GetHelpDetail()
        {
            return "传入操作类型、路径、文件名后缀、写入的XML文件名</br>" + "例如：{msgrecover, E:\\messsage, .htm,result.xml}</br>" + "返回：操作的最终结果";
        }

        public string[] GetParametersInfo()
        {
            string[] info = new string[4];
            info[0] = "操作名称，必须填写msgrecover";
            info[1] = "被操作的文件和写入的XML文件所在的目录路径";
            info[2] = "被操作的文件的后缀";
            info[3] = "写入的XML文件的文件名";
            return info;
        }
        #endregion
    }
}
